package com.yc.supermarket.service.impl;

import com.yc.supermarket.entity.ProductType;
import com.yc.supermarket.mapper.ProductTypeMapper;
import com.yc.supermarket.service.ProductTypeService;
import com.yc.supermarket.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductTypeServiceImpl implements ProductTypeService {

    private ProductTypeMapper mapper;

    @Override
    public Map<String, Object> findTypeById(ProductType t) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ProductTypeMapper.class);
        ProductType type = mapper.findTypeById(t.getTypeId());
        //创建hashmap存储数据
        Map<String, Object> m = new HashMap<>();
        if (null == type ) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", type);
        }
        MybatisUtil.close(session);
        return m;
    }

    @Override
    public Map<String, Object> findAll() {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ProductTypeMapper.class);
        List<ProductType> types = mapper.findAll();
        //创建hashmap存储数据
        Map<String, Object> m = new HashMap<>();
        if (null == types || types.isEmpty() ) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", types);
        }
        MybatisUtil.close(session);
        return m;
    }

}
